﻿using DataService.Common;
using DataService.Core.Log;
using DataService.Extensions;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace DataService.DBSql
{
    public class MySqlSql : DBSchemaSql
    {
        public override string GetSelectScalarCountSql<T>(Expression<Func<T, bool>> whereFunc, ref object paramValue, string fullTableName, string databaseName)
        {
            //超过百万数据时,count(1)会变吗，数据量越大count越慢,先这么查，优化阶段，从索引表查询数量
            //查询总行数
            //select table_rows from information_schema.tables where table_schema='{databaseName}' and table_name='{fullTableName}'
            ResolveExpress resolve = new ResolveExpress();
            var dic = resolve.ResolveExpression<T>(whereFunc, ParameterPrefix);
            if (!dic.ContainsKey("sql"))
            {
                throw new Exception("GetSelectSql解析参数错误");
            }
            string sql = $"SELECT Count(1) FROM {fullTableName} WHERE {dic["sql"].ToString()}";
            paramValue = dic["values"];
            TraceLog.WriteLine($"MySql执行{sql}", "执行语句");
            return sql;
        }
    }
}
